import Vue from 'vue'
import Router from 'vue-router'

import layout from '@/views/layout.vue'
import shop from '@/views/ali/shop/Shop.vue'
import follow from '@/views/ali/follow/Follow.vue'
import followList from '@/views/ali/follow/FollowList.vue'
import message from '@/views/ali/message/Message.vue'
import account from '@/views/ali/account/Account.vue'
import shopDetail from "@/views/ali/shop/ShopDetail.vue"
import order from '@/views/ali/order/Order.vue'
import cOrder from '@/views/ali/order/COrder.vue'
import showOrder from '@/views/ali/order/ShowOrder.vue'
import orderDetail from '@/views/ali/order/OrderDetail.vue'
import library from '@/views/ali/library/Library.vue'
import aliTott from '@/views/ali/library/AliTott.vue'
import Login from '@/views/login/login'
import ListGoods from '@/views/zj/goods/list'
import OrderList from '@/views/zj/order/list'
import OrderInfo from '@/views/zj/order/listInfo'
import Oauth from '@/views/zj/oauth/oauth'
import Add from '@/views/zj/goods/add'

import {Message} from 'element-ui'

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      name: '首页',
      component: layout,
      children: [
        {
          path: '/1688/order/corder',
          meta: "1688-tt",
          keepAlive: true,
          name: '创建字节订单',
          component: cOrder,
        },
        {
          path: '/1688/to-tt/:id',
          meta: "1688-tt",
          keepAlive: true,
          name: '上架',
          component: aliTott,
        },
        {
          path: '/1688/follow/list/:id',
          meta: "1688-关注",
          keepAlive: true,
          name: '列表',
          component: followList,
        },
        {
          path: '/message/1688',
          meta: "1688",
          keepAlive: true,
          name: '消息',
          component: message,
        },
        {
          path: '/1688/follow',
          meta: "1688",
          keepAlive: true,
          name: '关注',
          component: follow,
        },
        {
          path: '/1688/logistics/:id',
          meta: "1688",
          keepAlive: true,
          name: '物流',
          component: showOrder,
        },
        {
          path: '/1688/account',
          meta: "1688",
          keepAlive: true,
          name: '账户',
          component: account,
        },
        {
          path: '/1688/shop',
          meta: "1688",
          keepAlive: true,
          name: '1688选取商品',
          component: shop,
        },
        {
          path: '/1688/shop/detail/:id',
          meta: "1688-商品",
          keepAlive: false,
          name: '商品详情',
          component: shopDetail,
        },
        {
          path: '/1688/library',
          meta: "1688",
          name: '我的商品库',
          component: library,
        },
        {
          path: '/1688/order',
          meta: "1688",
          name: '我的订单',
          component: order,
        },
        {
          path: '/zj/goods/list',
          meta: "抖店",
          name: '商品列表',
          component: ListGoods,
        },
        {
          path: '/1688/order/:id',
          meta: "1688-订单",
          name: '订单结算',
          component: orderDetail,
        },
        {
          path: '/zj/order/list',
          meta: "抖店",
          name: '订单处理',
          component: OrderList,
        },
        {
          path: '/zj/order/listInfo',
          meta: "抖店",
          name: '订单信息',
          component: OrderInfo,
        },
        {
          path: '/zj/oauth/oauth',
          meta: "抖店",
          name: '授权',
          component: Oauth,
        },
        {
          path: '/zj/goods/add',
          meta: "抖店",
          name: '添加商品',
          component: Add,
        }
      ]
    },
    {
      path: '/login',
      name: 'Login',
      component: Login
    }
  ],
  mode: 'history'
});
//在路由配置生效前 统一判断accessToken
/** 路由守卫
 * to->要去的路由配置
 * from->当前的路由配置*/
router.beforeEach((to, from, next) => {
  console.log('to', to)
  //如果要去的是登录 -> next()
  if (to.path === '/login') {
    next();
  } else {
    //如果要去的不是登录 判断是否有accessToken
    const token = localStorage.getItem('token')
    if (!token) {
      //若果没有accessToken->login
      Message.warning('请重新登录')
      router.push({path: '/login'})
      return
    }
    //若果有accessToken->next()
    next()
  }
})
export default router
